﻿from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import getromid,time,json,re
from flask import request,Flask 

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
app=Flask(__name__)



@app.route('/getHotel',methods = ['GET'])
def return_hotel():
    hotelList=[]
    cityname=request.args.get("citynane")
    hotelname=request.args.get("hotelname")
    #得到时间数组
    startTime_for_unix=time.strptime(request.args.get("startTime"), "%Y-%m-%d")
    endTime_for_unix=time.strptime(request.args.get("endTime"), "%Y-%m-%d")

   #获取酒店id
    HotelId=getromid.getroomid(driver,cityname,hotelname)
    time.sleep(0.5)
    if HotelId==0:
        return "0"
    
    #拼接url
    HotelUrl="https://i.meituan.com/awp/h5/hotel/poi/deal.html?poiId=%s&startTime=%d&endTime=%d"%(HotelId,int(time.mktime(startTime_for_unix))*1000,int(time.mktime(endTime_for_unix)*1000))
    driver.get(HotelUrl)
    time.sleep(0.5)
    try:
        driver.find_element_by_xpath('//*[@id="main"]/section/section[7]/div[2]').click()
       

    except:
        pass


    time.sleep(1)
    temp=0
    for it in driver.find_elements_by_xpath('//*[@id="main"]/section/section[7]/ul/li'):
        datlist=it.text.split('\n')
        datdict={"roomname":datlist[temp],"breakfast":datlist[temp+1]}
        temp=temp+2
        if re.sub(r'\D',"",datlist[temp])=='' or '点' in datlist[temp]:
            temp=temp+1
            datdict["Price"]=int(re.sub(r'\D',"",datlist[temp]))
            temp=temp+1
        else:
            datdict["Price"]=int(re.sub(r'\D',"",datlist[temp]))
            temp=temp+1
             
        if '券减' in datlist[temp]:
            
            datdict["Originalprice"]=datdict["Price"]+int(re.sub(r'\D',"",datlist[temp]))
            print(datdict["Originalprice"])
            temp=temp+1
        if "仅剩" in datlist[temp]:
            temp=temp+1
        
        datdict['hasroom']=datlist[temp]

        temp=0
        hotelList.append(datdict)
    
    return json.dumps(hotelList,ensure_ascii=False)

if __name__=="__main__":

    app.run(host="127.0.0.1",port=8089)

    
